package com.young.aidl_base_client;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.young.aidl_base_sdk.ICalculator;
import com.young.aidl_base_sdk.ICalculatorListener;
import com.young.aidl_base_sdk.bean.MsgManager;
import com.young.aidl_base_sdk.bean.Sample;
import com.young.aidl_base_sdk.bean.Sample2;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    public static final String TAG = "Young-AIDL-Client-" + MainActivity.class.getSimpleName();

    // AIDL 接口
    private ICalculator mCalculator;

    private MsgManager mMsgManager = new MsgManager();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initClick();

        mMsgManager.getCalculator(this, new MsgManager.OnGetCalculatorListener() {
            @Override
            public void onGet(ICalculator calculator) {
                mCalculator = calculator;
                callListener();
            }
        });


    }


    private void initClick() {
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_do1:
//                        optionParcel();
//                        optionOut();
                        optionBundle();
                        break;
                    case R.id.btn_do2:
                        transactFileDescriptor();
                        break;
                    case R.id.btn_do3:
                        optionTimeOut();
                        break;
                    case R.id.btn_do4:
                        optionOneWay();
                        break;
                    case R.id.btn_do5:
                        callListener();
                        break;
                }

            }
        };

        findViewById(R.id.btn_do1).setOnClickListener(listener);
        findViewById(R.id.btn_do2).setOnClickListener(listener);
        findViewById(R.id.btn_do3).setOnClickListener(listener);
        findViewById(R.id.btn_do4).setOnClickListener(listener);
        findViewById(R.id.btn_do5).setOnClickListener(listener);
    }

    private void optionOneWay() {
        mMsgManager.getCalculator(this, new MsgManager.OnGetCalculatorListener() {
            @Override
            public void onGet(ICalculator calculator) {
                try {
                    calculator.optionOneWay();
                    Log.e(TAG, "optionOneWay 耗时方法调用完成了");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void optionTimeOut() {
        mMsgManager.getCalculator(this, new MsgManager.OnGetCalculatorListener() {
            @Override
            public void onGet(ICalculator calculator) {
                try {
                    calculator.optionTimeOut();
                    Log.e(TAG, "耗时方法调用完成了");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void transactFileDescriptor() {
        mMsgManager.getCalculator(this, new MsgManager.OnGetCalculatorListener() {
            @Override
            public void onGet(ICalculator calculator) {
                File file = new File(Environment.getExternalStorageDirectory(), "Video_2016-11-11_225450.avi");
                ParcelFileDescriptor fileDescriptor = null;
                try {
                    Log.e(TAG, "客户端 开始传输大文件 " + file.getName() + " " + file.length() + " " + System.currentTimeMillis());
                    fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
                    calculator.transactFileDescriptor(fileDescriptor);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                long k = 1694838290695L - 1694838285694L;
                Log.e(TAG, "k = " + k);
            }
        });


    }

    private void optionParcel() {
        mMsgManager.getCalculator(this, new MsgManager.OnGetCalculatorListener() {
            @Override
            public void onGet(ICalculator calculator) {
                Sample sample = new Sample();
                sample.setNum(999);
                sample.setSample2(new Sample2(33));
                try {
                    ApplicationInfo appInfo = getApplicationInfo();

                    String processName = appInfo.processName;
                    Log.e(TAG, "hello pid=" + processName);
                    calculator.optionParcel(sample);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });


    }

    private void optionOut() {
        mMsgManager.getCalculator(this, new MsgManager.OnGetCalculatorListener() {
            @Override
            public void onGet(ICalculator calculator) {
                Sample sample = new Sample();
                try {
                    Log.e(TAG, this + " optionOut before=" + sample);
                    calculator.optionOut(sample);
                    Log.e(TAG, this + " optionOut after=" + sample);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void optionBundle() {
        mMsgManager.getCalculator(this, new MsgManager.OnGetCalculatorListener() {
            @Override
            public void onGet(ICalculator calculator) {
                Bundle bundle = new Bundle();

                Sample sample = new Sample();
                sample.setNum(999);
                sample.setSample2(new Sample2(33));

                bundle.putParcelable("key1", sample);
                bundle.putSerializable("key2", new Sample2(66));

                try {

                    calculator.optionBundle(bundle);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });


    }

    private void callListener() {
        mMsgManager.getCalculator(this, new MsgManager.OnGetCalculatorListener() {
            @Override
            public void onGet(ICalculator calculator) {
                ICalculatorListener.Stub listener = new ICalculatorListener.Stub() {
                    @Override
                    public void onCallback(String result) throws RemoteException {
                        Log.e(TAG, "客户端接收到服务器主动返回的数据：" + result);
                    }
                };
                try {
                    calculator.registerListener(listener);
                    Log.e(TAG, "客户端注册了回调");

//            Thread.sleep(8000);
                    Thread.sleep(1000);
//            mCalculator.unRegisterListener(listener);
//            Log.e(TAG,"客户端注销了回调");
                } catch (RemoteException | InterruptedException e) {
                    Log.e(TAG, "客户端注册了回调失败 " + e.getMessage());
                }
            }
        });


    }
}